package mvc;


import mvc.bezier.BezierModel;
import mvc.helix.HelixModel;
import mvc.selix.SelixModel;
import mvc.tubolar.TubolarModel;

import org.geom.curve.Bezier;
import org.geom.curve.Helix;
import org.geom.curve.ICurve;
import org.geom.curve.SElix;
import org.geom.surface.TubolarSurface;

public class CurveManager {

	//-------------------------------------------------------- curve type
	public static final int SELI = 0;
	public static final int BEZIER = 1;
	public static final int HELIX = 2;
	public static final int CIRCLE = 3;
	
	//-------------------------------------------------------- classvars
	//-------------------------------------------------------- curves instances	
	
	// trunk
	private ICurve currentTrunk = null;
	private IModel currentTrunkModel = null;
	
	// strange helix
	private SElix seli = null;
	private SelixModel seliModel = null;
	
	// tubolar surface
	private TubolarModel tuboleModel = null;
	
	//possibles trunks
	private BezierModel bezier = null;
	private HelixModel helix = null;
	
	//-------------------------------------------------------- singleton
	private static CurveManager instance = null;
	private CurveManager() { 
	
	}
	public static CurveManager getInstance() {
		if(instance==null) instance = new CurveManager();
		return instance;
	}

	
	//-------------------------------------------------------- curve builder
	//-------------------------------------------------------- publics
	public SelixModel getSelix() {
		if(currentTrunk==null) getBezier(); // default trunk is bezier
		if(seliModel==null){
			seli = new SElix(currentTrunk);
			seliModel = new SelixModel(seli);
		}	
		else seli.setTrunk(currentTrunk);
		return seliModel;
	}
	
	//---------------------------------------------- trunks factory
	public BezierModel getBezier() {
		if(bezier==null) {
			Bezier bezierCurve = new Bezier(6);
			bezier = (BezierModel) new BezierModel(bezierCurve);
		}
		setTrunk(bezier);
		return bezier;
	}

	public HelixModel getHelix() {
		if(helix==null) {
			Helix helixCurve = new Helix();
			helix = new HelixModel(helixCurve);
		}
		setTrunk(helix);
		return helix;
	}

	//-------------------------------------------------------- switch trunk logics	
	public IModel getCurrentTrunkModel() {
		return currentTrunkModel;
	}
	
	private void setTrunk(IModel model) {
		currentTrunk = (ICurve) model.getData();
		currentTrunkModel = model;
		model.addParent(getSelix());
		if(seli!=null)
			seli.setTrunk(currentTrunk);
	}
	
	//-------------------------------------------------------- tubolar 
	public TubolarModel getSeliTubolar() {
		System.out.println("CurveManager > getSeliTubolar");
	
		if(tuboleModel==null) {
			TubolarSurface tube = new TubolarSurface(seli);
			tuboleModel = new TubolarModel(tube);
		}
		getSelix().addParent(tuboleModel);
		return tuboleModel;
	}
}
